package uriel.frankel;


import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class Preferences extends PreferenceActivity implements
		OnSharedPreferenceChangeListener,
		YesNoDialogPreference.YesNoDialogListener {

	SharedPreferences settings;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		
		settings = PreferenceManager.getDefaultSharedPreferences(this);
		settings.registerOnSharedPreferenceChangeListener(this);
		updateViews();
		
		enableOrDisableLeasingViews();
		Preference resetPreferences = getPreferenceManager().findPreference(
				"erasegame");
		if (resetPreferences != null) {
			YesNoDialogPreference yesNo = (YesNoDialogPreference) resetPreferences;
			yesNo.setListener(this);
		}
		
	}

	private void updateViews() {
		setSummeryfromPreferencesView("points", "2.25");
		setSummeryfromPreferencesView("no_social_edditions", "0");
		setSummeryfromPreferencesView("no_social_edditions_to_salary", "0");
		setSummeryfromPreferencesView("driving_refund", "0");
		setSummeryfromPreferencesView("social_percent", "100");
		setSummeryfromPreferencesView("pension_percent", "5");
		
		setSummeryfromPreferencesView("payment_for_company", "0");
		setSummeryfromPreferencesView("car_price", "0");
		//set default value for keren hishtalmut
		resetCheckbox("keren");
		resetCheckbox("hybrid");
	}

	private void resetCheckbox(String key) {
		((CheckBoxPreference)findPreference(key)).setChecked(settings.getBoolean(key, false /*false is because of an Android bug https://code.google.com/p/android/issues/detail?id=6641 */));
	}


	private void setSummeryfromPreferencesView(String viewName, String DefValue) {

		String value = settings.getString(viewName, DefValue);
		EditTextPreference editTextView = (EditTextPreference) findPreference(viewName);
		if (value.equals("")) {
			value = "0";
		}
		editTextView.setText(value);
		editTextView.setSummary(value);
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		updateViews();
		if (key.equals("leasing")) {
			enableOrDisableLeasingViews();
		}
	}

	private void enableOrDisableLeasingViews() {
		boolean enabled = settings.getBoolean("leasing",false);
		CheckBoxPreference leasing = (CheckBoxPreference) findPreference("leasing");
		leasing.setChecked(enabled);
		
		enableOrDisableView("car_price",enabled );
		enableOrDisableView("hybrid", enabled);
		enableOrDisableView("payment_for_company",
				enabled);
		
	}

	private void enableOrDisableView(String name, Boolean isEnabled) {
		Preference view = (Preference) findPreference(name);
		view.setEnabled(isEnabled);
	}

	/**
	 * if the user closes  the dialog of reset preferences
	 */
	public void onDialogClosed(boolean positiveResult) {
		if (positiveResult) {
			//save the version code
			int versionCode = settings.getInt("VERSION_CODE", 13);
			
			settings.edit().clear().commit();
			PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
			enableOrDisableLeasingViews();
			
			//restore the version code
			settings.edit().putInt("VERSION_CODE", versionCode).commit();
		}
	}
}
